package org.moonshiner.controller.web;

import java.io.File;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.moonshiner.controller.service.ProjectService;
import org.moonshiner.controller.service.UserService;
import org.moonshiner.model.constants.ErrorMessages;
import org.moonshiner.model.project.Project;
import org.moonshiner.model.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping(value = "/project")
public class ProjectController {

	@Autowired
	private UserService userService;

	@Autowired
	private ProjectService projectService;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String newProjectWizard(ModelMap model) {

		model.addAttribute("project", new Project());

		List<String> algorithmList = new ArrayList<String>();
		algorithmList.add("DES");
		algorithmList.add("AES");

		model.addAttribute("algList", algorithmList);

		return "project/newProjectWizard";
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String createProject(@ModelAttribute("project") Project project,
			BindingResult result, SessionStatus status, Principal principal) {

		User user = userService.getUserByName(principal.getName());

		project.setUser(user);

		projectService.save(project);

		return "redirect:project/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String listProjects(ModelMap model, Principal principal) {

		model.addAttribute("projects",
				userService.getProjectsByUserName(principal.getName()));

		return "project/projectsList";
	}

	@RequestMapping(value = "/addFile", method = RequestMethod.GET)
	public String addNewFile(
			@RequestParam(value = "pid", required = true) String pid,
			ModelMap model) {

		List<String> errors = new ArrayList<String>();

		try {
			Integer.parseInt(pid);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			errors.add(ErrorMessages.INVALID_PROJECT_ID);
		}

		return "project/addFile";
	}

	@RequestMapping(value = "/addFile", method = RequestMethod.POST)
	public String addFile(HttpServletRequest request) {

		String sourceCode = request.getParameter("sourceCode");
		String fileName = request.getParameter("fileName");
		String id = request.getParameter("pid");
		List<String> errors = new ArrayList<String>();

		Integer projectId = null;
		try {
			projectId = Integer.parseInt(id);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			errors.add(ErrorMessages.INVALID_PROJECT_ID);
		}

		Project project = projectService.get(projectId);

		if (project != null) {
			projectService.addFileToProject(project, fileName, sourceCode,
					project.getPassword());
		} else if (!errors.contains(ErrorMessages.INVALID_PROJECT_ID)) {
			errors.add(ErrorMessages.INVALID_PROJECT_ID);
		}

		return "redirect:/project/projectFiles?pid=" + project.getId();
	}

	@RequestMapping(value = "/files", method = RequestMethod.GET)
	public String projectFiles(
			@RequestParam(value = "pid", required = true) String pid,
			ModelMap model,
			@RequestParam(value = "path", required = false) String path) {

		List<String> errors = new ArrayList<String>();
		Integer projectId = null;

		try {
			projectId = Integer.parseInt(pid);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			errors.add(ErrorMessages.INVALID_PROJECT_ID);
		}

		Project project = projectService.get(projectId);
		if (path == null) {
			path = "";
		}
		List<File> files = projectService.getFilesList(project, path);

		model.addAttribute("files", files);
		model.addAttribute("project", project);

		return "project/projectFiles";
	}

	@RequestMapping(value = "/showFile", method = RequestMethod.GET)
	public String showFile(HttpServletResponse response,
			@RequestParam(value = "path", required = true) String path,
			@RequestParam(value = "pid", required = true) String pid) {

		List<String> errors = new ArrayList<String>();

		Integer id = null;
		try {
			id = Integer.parseInt(pid);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			errors.add(ErrorMessages.INVALID_PROJECT_ID);
			return "project/projectFiles";
		}

		Project project = projectService.get(id);

		projectService.writeFileToResponse(response, project, path);

		return null;
	}
}
